﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;

namespace TeamService
{
    class MaxStateConverter : IValueConverter
    {
        public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            var str = (WindowState)value;
            return str == WindowState.Maximized ? 1 : 2;
        }

        public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            return WindowState.Maximized;
        }
    }
    class ResizeModeToVisibility : IValueConverter
    {
        public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            var mode = (ResizeMode)value;
            return mode == ResizeMode.NoResize ? Visibility.Collapsed : Visibility.Visible;
        }

        public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            var v = (Visibility)value;
            return v == Visibility.Collapsed ? ResizeMode.NoResize : ResizeMode.CanResize;
        }
    }
    class BoolToVisibility : IValueConverter
    {
        public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            var v = (bool)value;

            return v == true ? Visibility.Visible : Visibility.Collapsed;
        }

        public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            var v = (Visibility)value;
            return v == Visibility.Collapsed ? false : true;
        }
    }
    class IntToVisibility : IValueConverter
    {
        public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            var i = (int)value;
            return i > 1 ? Visibility.Visible : Visibility.Collapsed;
        }

        public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    public class CommandBase : ICommand
    {
        Func<object, bool> _canExecute;
        Action<object> _execute;
        public CommandBase(Func<object, bool> canExecute, Action<object> execute)
        {
            _canExecute = canExecute;
            _execute = execute;
        }
        public event EventHandler CanExecuteChanged;

        public Boolean CanExecute(Object parameter)
        {
            return _canExecute(parameter);
        }

        public void Execute(Object parameter)
        {
            _execute(parameter);
        }
    }
    public class ConverterBase : IValueConverter
    {
        Func<object, object> _back,_forward;
        public ConverterBase(Func<object,object> forward,Func<object,object>back)
        {
            _back = back;
            _forward=forward;
        }
        public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            return _forward(value);
        }

        public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            return _back(value);
        }
    }



    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public static ICommand CloseCommand { get; set; } = new CommandBase(
            (o1) => { return true; },
            (o2) =>
            {
                if (o2 is Window)
                    ((Window)o2).Close();
            });


        public static ICommand MaxCommand { get; set; } =
        new CommandBase(
            (o1) => { return true; },
            (o2) =>
            {
                if (o2 is Window)
                {
                    var w = (Window)o2;
                    w.WindowState = w.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
                }
            });

        public static ICommand MinCommand { get; set; } = new CommandBase(
            (o1) => { return true; },
            (o2) =>
            {
                if (o2 is Window)
                    ((Window)o2).WindowState = WindowState.Minimized;
            });

        protected override void OnStartup(StartupEventArgs e)
        {

            base.OnStartup(e);
        }
    }
}
